package com.zc.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zc.common.Result;
import com.zc.exception.CustomerException;
import com.zc.pojo.Exams;
import com.zc.pojo.Students;
import com.zc.service.ExamsService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/exams")
public class ExamsController {

    @Resource
    private ExamsService examsService;


    @GetMapping("/page")
    public Result selectAll(@RequestParam Integer pageNum,
                            @RequestParam Integer pageSize,
                            @RequestParam(required = false) String name) {
        Page<Exams> examsPage = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<Exams> lqw = new LambdaQueryWrapper<>();
        lqw.orderByDesc(Exams::getExamId);
        if (name != null && !name.trim().isEmpty()) {
            lqw.like(Exams::getExamName, name);
        }
        return Result.success(examsService.page(examsPage, lqw));
    }

    @GetMapping("/{id}")
    public Result selectById(@PathVariable Integer id) {
        return Result.success(examsService.getById(id));
    }


    @PostMapping("/add")
    public Result save(@RequestBody Exams exams) {
        if (exams.getExamName()!= null && !exams.getExamName().trim().isEmpty()) {
            LambdaQueryWrapper<Exams> nameWrapper = new LambdaQueryWrapper<>();
            nameWrapper.eq(Exams::getExamName, exams.getExamName().trim());
            long nameCount = examsService.count(nameWrapper);
            if (nameCount > 0) {
                throw new CustomerException("考试已存在，请更换考试名称");
            }
        }
        return Result.success(examsService.save(exams));
    }

    @GetMapping("/getExams")
    public Result getExams() {
        return Result.success(examsService.list());
    }



    @PutMapping("/update")
    public Result updateById(@RequestBody Exams exams) {
        Exams existingExam = examsService.getById(exams.getExamId());

        if (exams.getExamName() != null && !exams.getExamName().equals(existingExam.getExamName())) {
            LambdaQueryWrapper<Exams> nameWrapper = new LambdaQueryWrapper<>();
            nameWrapper.eq(Exams::getExamName, exams.getExamName())
                    .ne(Exams::getExamId, exams.getExamId());

            long nameCount = examsService.count(nameWrapper);
            if (nameCount > 0) {
                throw new CustomerException("考试已存在，请更换考试名称");
            }
        }
        return Result.success(examsService.updateById(exams));
    }


    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id) {
        return Result.success(examsService.removeById(id));
    }

    @PostMapping("/deleteBatch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        return Result.success(examsService.removeBatchByIds(ids));
    }
}

